package search.service.analysis;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;
import common.dao.elastic.ElasticSearchDao;
import common.helper.json.JsonToJavaObject;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by julianzliu on 2016/7/2.
 */
@Component
public class AnalysisService{
    final Logger logger = LoggerFactory.getLogger(AnalysisService.class);

    private  ElasticSearchDao elasticsearchDao;

    private  ObjectMapper mapper;

    @Inject
    public AnalysisService(ElasticSearchDao elasticsearchDao, ObjectMapper mapper ) {
        this.elasticsearchDao = elasticsearchDao;
        this.mapper = mapper;
    }


    //AnalyzeResponse analyzeResponse = client.admin().indices().prepareAnalyze("trying out").execute().actionGet();

    /*************************************
     *
     * @param esDao
     * @param text
     * @param analyzer
     * @return
     */
    public static List<AnalyzeResponse.AnalyzeToken> getTokenList(ElasticSearchDao esDao, String text, String analyzer){
        AnalyzeResponse analyzeResponse = esDao.analyze(text,analyzer);
        List<AnalyzeResponse.AnalyzeToken> tokenList =  new ArrayList<>();
        for (AnalyzeResponse.AnalyzeToken analyzeToken : analyzeResponse.getTokens()) {
            tokenList.add(analyzeToken);
        }
        return tokenList;
    }

    /***************************************
     *
     * @param esDao
     * @param text
     * @param analyzer
     * @return
     */
    public static List<String> getTokenTermList(ElasticSearchDao esDao, String text, String analyzer){
        List<AnalyzeResponse.AnalyzeToken> tokenList = getTokenList(esDao, text, analyzer);
        List<String> tokenTermList = new ArrayList<>();
        for(AnalyzeResponse.AnalyzeToken analyzeToken : tokenList){
            tokenTermList.add(analyzeToken.getTerm());
        }
        return tokenTermList;
    }




    /**************************************************************************************************
     *
     * @param text
     * @param analyzer
     * @return
     */
    public String analyze(String text, String analyzer){
        List<AnalyzeResponse.AnalyzeToken> tokenList = getTokenList(this.elasticsearchDao, text, analyzer);
        ArrayNode arrNode = this.mapper.createArrayNode();
        for(AnalyzeResponse.AnalyzeToken token : tokenList){
            ObjectNode tokenNode = this.mapper.createObjectNode();
            tokenNode.put("token", token.getTerm());
            tokenNode.put("start_offset", token.getStartOffset());
            tokenNode.put("end_offset" , token.getEndOffset());
            tokenNode.put("type" , token.getType());
            tokenNode.put("position", token.getPosition());
            arrNode.add(tokenNode);
        }
        ObjectNode retNode = this.mapper.createObjectNode();
        retNode.put("tokens" , arrNode);
        //return retNode.toString();
        String retJson = JsonToJavaObject.jsonNodeToString(retNode);
        return retJson;
    }



    /**************************************
     *
     * @param args
     */
    public static void main(String[] args){
        /*
        1）
        */
        List<AnalyzeResponse.AnalyzeToken> tokens = getTokenList(new ElasticSearchDao("debug"), "北京市车流" , "ik_smart" );
        for(AnalyzeResponse.AnalyzeToken analyzeToken : tokens){
            System.out.println(
                            "term: " + analyzeToken.getTerm() + "\n"
                            + "type: " + analyzeToken.getType() + "\n"
                            + "position: " + analyzeToken.getPosition() + "\n"
                            + "start: " + analyzeToken.getStartOffset() + "\n"
                            + "end: " + analyzeToken.getEndOffset() + "\n"
            );
        }

        /*
        2）
         */
        AnalysisService service = new AnalysisService(new ElasticSearchDao("debug"), new ObjectMapper());
        String retJson = service.analyze("北京市车流", "ik_smart");
        System.out.println(retJson);
    }

}
